C.................... F107-KP-DDD-reader.for .......................
C....  This program processes the F10KP-DDD data file for the program. The output 
C....  rewrites the Kp file as a function of UT rather than 8 values per line
C.... Written by P. Richards November 2008
      IMPLICIT NONE
      INTEGER I,J,K,L         !.. loop control variables
	INTEGER YYYY,MM,DD,DDD,YYYYDDD,F107,F107A
	REAL UTHRS,UTSTART,YSTART,KP(8),YSTOP
      INTEGER KPTOAP(0:90)
	REAL APK(99999),AP(7)

      DATA AP/7*-99/
      !.. Conversion factors from Kp = 1..9. AP = KPTOAP(KP)
      !.. Kp= 0    1    2   3    4    5    6     7     8    9
      !.. AP= 0,   3,   7,  15,  27,  48,  80,  140,  240, 400
      !.. 90 Ap values for easy conversion to Ap
      DATA KPTOAP/3*0,4*2,3*3,3*4,4*5,3*6,3*7,4*9,3*12,3*15,4*18,3*22
     >   ,3*27,4*32,3*39,3*49,4*56,3*67,3*80,4*94,3*111,3*132,4*154
     >   ,3*179,3*207,4*236,3*300,400/

      WRITE(6,676)
 676  FORMAT(/6X,'....... F107-KP-DDD-reader.for ......')


      CALL OPEN_FILE()      !... open files assigned in batch (.bat) file

      READ(5,*)  UTSTART    !.. Start UT to compare with FLIP
      READ(5,*)  YSTART     !.. Start Year (YYYYddd) to compare with FLIP
      READ(5,*)  YSTOP    !.. Start Year (YYYYddd) to compare with FLIP
	UTHRS=UTSTART

      !.. Write headers in output file and get first day of data
      WRITE(4,291) 
 291   FORMAT('    UThrs  YYYYDDD  DDD YYYY MM DD F107 F107A    Kp'
     >  ,4X,'Ap1    AP2    AP3    AP4    AP5    AP6    AP7  AP2_5')
      WRITE(7,*) '   UThrs  YYYYDDD YYYY MM DD   Kp    F107 F107A'

      K=0
      !.. read headers off the input file
 10   READ(1,*,ERR=10,END=90) YYYY,MM,DD,(KP(J),J=1,8),F107,F107A

      !.. Read in the AE data
      DO I=1,9999999
	  !.. Convert year, month, day to YYYYddd format
        CALL CONV_DATE(DDD,YYYY,MM,DD)
        YYYYDDD=YYYY*1000+DDD 
        IF(YYYYDDD.GT.YSTOP) STOP

          !.. Get daily average Ap
          AP(1)=0.0
          DO J=1,8
            AP(1)=AP(1)+KPTOAP(KP(J))
          ENDDO
          AP(1)=AP(1)/8.0

        !.. Now write the Kp at 3 hour intervals 
        IF(YYYYDDD.GE.YSTART) THEN
          DO J=1,8
            K=K+1
            APK(K)=KPTOAP(KP(J))  !.. running average Ap
            AP(2)=APK(K)
            IF(K.GT.1) AP(3)=APK(K-1)
            IF(K.GT.2) AP(4)=APK(K-2)
            IF(K.GT.3) AP(5)=APK(K-3)
            IF(K.GT.12) AP(6)=(APK(K-6)+APK(K-7)+APK(K-8)+APK(K-9)+
     >          APK(K-10)+APK(K-11)+APK(K-12)+APK(K-12))/8.0
            IF(K.GT.19) AP(7)=(APK(K-12)+APK(K-13)+APK(K-14)+APK(K-15)+
     >          APK(K-16)+APK(K-17)+APK(K-18)+APK(K-19))/8.0

            WRITE(4,'(F10.2,I8,2I5,2I3,2I5,22F7.2)') 
     >        UTHRS,YYYYDDD,DDD,YYYY,MM,DD,F107,F107A,KP(J)/10,
     >         (AP(L),L=1,7),(AP(2)+AP(3)+AP(4)+AP(5))/4.0
            !.. output  for histograms
            WRITE(7,'(F10.2,I8,I5,2I3,F8.2,2I5)') 
     >          UTHRS,YYYYDDD,YYYY,MM,DD,KP(J)/10,F107,F107A
            WRITE(7,'(F10.2,I8,I5,2I3,F8.2,2I5)') 
     >          UTHRS+2.99,YYYYDDD,YYYY,MM,DD,KP(J)/10,F107,F107A
            UTHRS=UTHRS+3.0
          ENDDO
        ENDIF
        !.. Get the next day of data
	  READ(1,*,ERR=90,END=90) YYYY,MM,DD,(KP(J),J=1,8),F107,F107A
      ENDDO

 90   CONTINUE
 190  FORMAT(F8.3,F8.3,I6,I6,I7,I7)
 191  FORMAT(F8.1,F8.2,F8.2,F7.3,F7.3,I5,I5,I6,I6,F7.2,1P,2E10.2,9F10.2)
      STOP
      END

C::::::::::::::::::::::::: SUBROUTINE CONV_DATE :::::::::::::::::::::::::::
C---- Scot A Braze,  8/7/95
C---- This subroutine converts the date from YYYY MM DD to YYYY DDD. 
C
      SUBROUTINE CONV_DATE(DDD,YYYY,MM,DD)
      IMPLICIT NONE
      INTEGER DDD,MM,DD,NCONV(12),LYCONV(12),YYYY
      LOGICAL DEBUG
      DATA DEBUG /.FALSE./
C
      DATA NCONV/0,31,59,90,120,151,181,212,243,273,304,334/
      DATA LYCONV/0,31,60,91,121,152,182,213,244,274,305,335/
C
      IF(MOD(YYYY,4).EQ.0) THEN
          DDD=LYCONV(MM)+DD
      ELSE
          DDD=NCONV(MM)+DD
      ENDIF
      IF(DEBUG) WRITE(6,*) YYYY,DDD
      END

